#include <iostream>
#include <cstdlib>
#include "threadpool.h"

std::mutex mtx;

void func(int i) {
    std::unique_lock<std::mutex> lock(mtx);
    // 让输出完整
    std::cout << "Task " << i << " started in thread:" << std::this_thread::get_id() << std::endl;
    lock.unlock();

    int secs = rand() % 10;
    std::this_thread::sleep_for(std::chrono::seconds(secs));

    lock.lock();
    std::cout << "Task " << i << " finished in " << secs << " seconds" << std::endl;
    lock.unlock();
}

int main(int argc, char *argv[]) {
    Threadpool t_pool(20);

    int i = 0;
    while (i < 40) {
        i++;
        auto task = std::bind(func, i);
        t_pool.addTask(task);
    }
    
    t_pool.finish();
    
    return 0;
}
